//
//  AppDelegate.m
//  TWApp
//
//  Created by line0 on 13-7-6.
//  Copyright (c) 2013年 makeLaugh. All rights reserved.
//

#import "AppDelegate.h"
#import "ICSDrawerController.h"
#import "LeftViewController.h"
#import "MainViewController.h"
#import "RightViewController.h"
#import "Reachability.h"
#import "UserInfo.h"
#import "Glob.h"
#import "ViewController.h"
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [_window makeKeyAndVisible];
    // Added Introduction View Controller
    
    [self checkNetworkReachability];
    if (IOS7) {
//        [UINavigationBar appearance].barTintColor = [UIColor blueColor];
        [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
        [[UINavigationBar appearance] setTranslucent:NO];
        [[UINavigationBar appearance ] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
                                                                [UIColor orangeColor] , NSForegroundColorAttributeName,
                                                                
                                                                [UIFont fontWithName:@"Helvetica-Bold" size:21.0], NSFontAttributeName, nil]];
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"top_bg_ios7.png"] forBarPosition:UIBarPositionTopAttached barMetrics:UIBarMetricsDefault];
        
    }
    else
    {
        
        [[UINavigationBar appearance] setBackgroundColor:mRGBColor(215, 30, 40)];
//        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"top_bg.png"] forBarMetrics:UIBarMetricsDefault];
    }
    
//    rootViewController *viewController = [[rootViewController alloc] initWithNibName:@"rootViewController" bundle:nil];
    ViewController *viewController = [[ViewController alloc] initWithNibName:nil bundle:nil];
    
//    LeftViewController *viewController = [[LeftViewController alloc] initWithNibName:nil bundle:nil];
//    
//    //             MainViewController *viewController1 = [[MainViewController alloc] initWithNibName:nil bundle:nil];
//    
//    LeftViewController *viewController2 = [[LeftViewController alloc] initWithNibName:nil bundle:nil];
//    
//    RightViewController *viewController3 = [[RightViewController alloc] initWithNibName:nil bundle:nil];
    //
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    //
    [self.window setRootViewController:navigationController];
//    CGSize size = [[UIScreen mainScreen] bounds].size;
//    splashView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, size.width, size.height)];
//    [splashView setImage:[UIImage imageNamed:@"Default-568h"]];
//    
//    [self.window addSubview:splashView];
//    [self.window bringSubviewToFront:splashView];
    
//    [self performSelector:@selector(scale_1) withObject:nil afterDelay:0.0f];
//    [self performSelector:@selector(scale_2) withObject:nil afterDelay:0.5f];
//    [self performSelector:@selector(scale_3) withObject:nil afterDelay:1.0f];
//    [self performSelector:@selector(scale_4) withObject:nil afterDelay:1.5f];
//    [self performSelector:@selector(scale_5) withObject:nil afterDelay:2.0f];
//    [self performSelector:@selector(showWord) withObject:nil afterDelay:2.5f];
    
    
    return YES;
}

//-(void)scale_1
//{
//    UIImageView *round_1 = [[UIImageView alloc]initWithFrame:CGRectMake(100, 240, 15, 15)];
//    round_1.image = [UIImage imageNamed:@"bg"];
//    [splashView addSubview:round_1];
//    [self setAnimation:round_1];
//}
//
//-(void)scale_2
//{
//    UIImageView *round_2 = [[UIImageView alloc]initWithFrame:CGRectMake(105, 210, 20, 20)];
//    round_2.image = [UIImage imageNamed:@"round_"];
//    [splashView addSubview:round_2];
//    [self setAnimation:round_2];
//}
//
//-(void)scale_3
//{
//    UIImageView *round_3 = [[UIImageView alloc]initWithFrame:CGRectMake(125, 170, 30, 30)];
//    round_3.image = [UIImage imageNamed:@"bg"];
//    [splashView addSubview:round_3];
//    [self setAnimation:round_3];
//}
//
//-(void)scale_4
//{
//    UIImageView *round_4 = [[UIImageView alloc]initWithFrame:CGRectMake(160, 135, 40, 40)];
//    round_4.image = [UIImage imageNamed:@"bg"];
//    [splashView addSubview:round_4];
//    [self setAnimation:round_4];
//}
//
//-(void)scale_5
//{
//    UIImageView *heart_1 = [[UIImageView alloc]initWithFrame:CGRectMake(130, 180, 100, 100)];
//    heart_1.image = [UIImage imageNamed:@"bg"];
//    [splashView addSubview:heart_1];
//    [self setAnimation:heart_1];
//}
//
//-(void)setAnimation:(UIImageView *)nowView
//{
//    
//    [UIView animateWithDuration:0.6f delay:0.0f options:UIViewAnimationOptionCurveLinear
//                     animations:^
//     {
//         // 执行的动画code
//         [nowView setFrame:CGRectMake(nowView.frame.origin.x- nowView.frame.size.width*0.1, nowView.frame.origin.y-nowView.frame.size.height*0.1, nowView.frame.size.width*1.2, nowView.frame.size.height*1.2)];
//     }
//                     completion:^(BOOL finished)
//     {
//         // 完成后执行code
//         [nowView removeFromSuperview];
//         
//         
//     }
//     ];
//    
//    
//}

//-(void)showWord
//{
//    
//    UIImageView *word_ = [[UIImageView alloc]initWithFrame:CGRectMake(75, 440, 170, 29)];
//    word_.image = [UIImage imageNamed:@"bg"];
//    [splashView addSubview:word_];
//    
//    word_.alpha = 0.0;
//    [UIView animateWithDuration:1.0f delay:0.0f options:UIViewAnimationOptionCurveLinear
//                     animations:^
//     {
//         word_.alpha = 1.0;
//     }
//                     completion:^(BOOL finished)
//     {
//         // 完成后执行code
//         [NSThread sleepForTimeInterval:1.0f];
//         [splashView removeFromSuperview];
//         
//         
//         NSArray *coverImageNames = @[@"img_index_01txt", @"img_index_02txt", @"img_index_03txt"];
//         NSArray *backgroundImageNames = @[@"img_index_01bg", @"img_index_02bg", @"img_index_03bg"];
//         self.introductionView = [[ZWIntroductionViewController alloc] initWithCoverImageNames:coverImageNames backgroundImageNames:backgroundImageNames];
//         
//         [self.window addSubview:self.introductionView.view];
//         
//         __weak AppDelegate *weakSelf = self;
//         self.introductionView.didSelectedEnter = ^() {
//             [weakSelf.introductionView.view removeFromSuperview];
//             weakSelf.introductionView = nil;
//             
//             // enter main view , write your code ...
//             
//             LeftViewController *left = [[LeftViewController alloc] initWithNibName:nil bundle:nil];
//             //        RightViewController * right = [[RightViewController alloc]init];
//             
//             //        WWSideslipViewController * slide = [[WWSideslipViewController alloc]initWithLeftView:left andMainView:main andRightView:right andBackgroundImage:[UIImage imageNamed:@"bg.png"]];
//             //
//             //        //滑动速度系数
//             //        [slide setSpeedf:0.5];
//             //
//             //        //点击视图是是否恢复位置
//             //        slide.sideslipTapGes.enabled = YES;
//             
//             
//             ViewController *viewController1 = [[ViewController alloc] initWithNibName:nil bundle:nil];
//             
//             
//             LeftViewController *viewController = [[LeftViewController alloc] initWithNibName:nil bundle:nil];
//             
////             MainViewController *viewController1 = [[MainViewController alloc] initWithNibName:nil bundle:nil];
//             
//             LeftViewController *viewController2 = [[LeftViewController alloc] initWithNibName:nil bundle:nil];
//             
//             RightViewController *viewController3 = [[RightViewController alloc] initWithNibName:nil bundle:nil];
////
//             UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
////
//             UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
//             
//             UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
//             
//             UINavigationController *navigationController3 = [[UINavigationController alloc] initWithRootViewController:viewController3];
////
////             
//             weakSelf.tabBarController=[[RDVTabBarController alloc] init];
//             //    [tabBarController setViewControllers:@[photowall1, upload1,activities1,
//             //                                           order1]];
//             [weakSelf.tabBarController setViewControllers:@[navigationController, navigationController1,navigationController2,navigationController3]];
//             [weakSelf customizeTabBarForController:weakSelf.tabBarController];
////
////             
//             ICSDrawerController *drawer = [[ICSDrawerController alloc] initWithLeftViewController:left centerViewController:weakSelf.tabBarController];
//             [weakSelf.window setRootViewController:drawer];
//             
//             viewController1.tabBarItem.badgeValue = @"5";
//             
//         };
//     }
//     ];
//}

//#pragma mark - Methods
//- (void)customizeTabBarForController:(RDVTabBarController *)tabBarController {
//    NSArray *tabBarItemImages = @[@"zpq", @"sc", @"hd",@"dg"];
//    NSInteger index = 0;
//    for (RDVTabBarItem *item in [[self.tabBarController tabBar] items]) {
//        //[item setBackgroundSelectedImage:finishedImage withUnselectedImage:unfinishedImage];
//        UIImage *selectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@b",
//                                                      [tabBarItemImages objectAtIndex:index]]];
//        UIImage *unselectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@",
//                                                        [tabBarItemImages objectAtIndex:index]]];
//        [item setFinishedSelectedImage:selectedimage withFinishedUnselectedImage:unselectedimage];
//        [item setBadgeValue:@"9"];
//        [item setBadgeBackgroundImage:[UIImage imageNamed:@"sc.png"]];
//        [item setBadgeTextColor:[UIColor redColor]];
//        [item setBadgeTextFont:[UIFont systemFontOfSize:20]];
//        index++;
//    }
//}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

/**
 *  检测网络状态
 */
- (void)checkNetworkReachability
{
    Reachability *reach = [Reachability reachabilityWithHostname:@"http://www.baidu.com"];
    
    reach.reachableBlock = ^(Reachability *reach)
    {
        NSLog(@"REACHABLE!");
        [UserInfo sharedUser].isReachable = YES;
        NSString *yesOrNoAuto=[[NSUserDefaults standardUserDefaults] objectForKey:@"isAutoLogin"] ;
        if (yesOrNoAuto) {
            if ([yesOrNoAuto isEqualToString:@"yes"]) {
//                [self autoLogin];
            }
        }
        
    };
    
    reach.unreachableBlock = ^(Reachability *reach)
    {
        NSLog(@"UNREACHABLE!");
        [UserInfo sharedUser].isReachable = NO;
    };
    
    [reach startNotifier];
}
@end
